package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Business;
import cn.wolfcode.qo.BusinessQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.FileUploadUtil;
import cn.wolfcode.util.OpenApiUtil;
import cn.wolfcode.util.RequiredPermission;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;

@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private IBusinessService businessService;


    @RequiredPermission(name="门店页面",expression="business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessQueryObject qo){
        PageInfo<Business> pageInfo = businessService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        return "business/list";
    }


    @RequiredPermission(name="门店删除",expression="business:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           businessService.delete(id);
        }
        return "redirect:/business/list";
    }

    @Autowired
    private ServletContext selectContext;

    @RequiredPermission(name="门店新增/编辑",expression="business:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file) {
        //判断有没有图片文件
        if (file != null && file.getSize()>0){
            //更改图片后把之前的删掉
            String licenseImg = business.getLicenseImg();
            //获取一个项目的真实路径 即是:D:\java\IDEA2019\IdeaProjects\car-projects-01\src\main\webapp\  项目路径下的webapp
        String contextPath = selectContext.getRealPath("/");
        System.out.println(contextPath);
        //用工具进行图标保存l
        try {
            //如果之前有文件就先删除
            if (StringUtils.hasText(licenseImg)){
                FileUploadUtil.deleteFile(contextPath+licenseImg);
            }
            String url = FileUploadUtil.uploadFile(file, contextPath);


            //判断这个图片是不是营业执照
            JSONObject jsonObject = OpenApiUtil.sendRequest(new File(contextPath, url));
            System.out.println(jsonObject);
            //把这个图片路径设置到对象中 /upload/410c78e9-c08d-4cfd-b98c-ed326f689b35.png
            business.setLicenseImg(url);
        } catch (Exception e) {
            e.printStackTrace();
            }
        }

        if (business.getId() == null) {
            businessService.save(business);
        } else {
            businessService.update(business);
        }
        return "redirect:/business/list";
    }


    @RequiredPermission(name="门店新增/编辑",expression="business:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("business",businessService.get(id));
        }
        return "business/input";
    }
}
